#include <bits/stdc++.h>
using namespace std;
using uint=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;
const int N=1e6+5;

void solve(){
    
    return;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n,t;
    cin>>n>>t;
    vector<vector<int>> checkh(n+1);//储存行
    vector<vector<int>> checkl(n+1);//储存列
    vector<int> checkzd;//储存正对角线
    vector<int> checkfd;//储存负对角线
    for(int i=1;i<=t;i++){
        int a;
        cin>>a;
        a--;
        checkh[a/n+1].push_back(1);
        checkl[a%n+1].push_back(1);
        if(a/n+1==a%n+1)checkzd.push_back(1);
        if(a/n+1+a%n+1==n+1)checkfd.push_back(1);
        if(checkh[a/n+1].size()==n||checkl[a%n+1].size()==n||checkzd.size()==n||checkfd.size()==n){
            cout<<i<<endl;
            return 0;
        }
    }
    cout<<"-1"<<endl;

    return 0;
}